<?php
/*
Plugin Name: Megatop
Plugin URI: http://vmrf.ru
Description: Show mega top news
Author: vmrf.ru
Version: 1.0
Author URI: http://vmrf.ru
 */

namespace AY\Widget;

class User extends \AY\Widget\AWidget
{
    protected $widgetName = 'User';
    protected $widgetOptions = array(
        'classname' => 'user-ay',
        'description'   => 'widget show user info'
        );

    function widget( $args, $instance ) {
    ?>
    <script>
        $(document).ready(function(){
            jQuery.post('<?php echo ay()->config()->AJAXHost; ?>', {action: 'User'},function(data){
                data=jQuery.parseJSON(data);
                if (data.user){
                data.data=data.data.replace("returnaddress", window.location.href);
                jQuery("#current_user_info").html(data.data);
    $('#signin').click(function(){
        $('#signin-window').fadeToggle('fast')
    });
                }
        })});
    </script>
    <?php
    }

    function form( $instance ) {
    }

    function update($new_instance, $old_instance) {
    }

    public function ajax() {
        global $current_user;
        $current_user;

        if ($current_user->ID) {
	  $balans = count_user_posts($current_user->ID)*10;  
		//var_dump($balans);die();
		
            $data = 'Привет, <a class="ico ico_userName" href="/wp-admin/profile.php">' . $current_user->display_name . '</a>, ваш баланс <b>' . $balans . '</b> руб.&nbsp;&nbsp; <a href="/wp-admin/post-new.php?post_type=post" class="ico ico_userPublish">Опубликовать новость</a> '.
               '<a href="'.wp_logout_url("returnaddress").'" class="ico ico_userLogin">Выйти</a>';
        }
                                            
        return array('user' => $current_user->ID,
                     'data' => $data);
    }
}
